package com.xiaotu.controller;

import com.xiaotu.model.*;
import com.xiaotu.service.StarTvService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @作者 王艳龙
 * @日期 2021-01-20 13:59
 * @功能
 */
@CrossOrigin
@RestController
public class StarTvController {

    private Logger logger = Logger.getLogger(StarTvController.class);

    @Autowired
    private StarTvService starTvService;


    /**
     * 获取艺人信息
     * @param paramModel
     * @return
     */
    @GetMapping("/queryStarInfo")
    public Map<String, Object> queryStarInfo(ParamModel paramModel) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int success = 0;
        String message = "";
        try {
            //校验账户是否存在
            String verifyStatus = isVerifyPass(paramModel);
            if(StringUtils.isNotEmpty(verifyStatus)){
                throw new IllegalArgumentException(verifyStatus);
            }
            //设置默认page=1，rows=100
            setModelParam(paramModel);
            //获取列表记录
            List<StarModel> starList = starTvService.getStarInfo(paramModel);
            resultMap.put("data", starList);
            //获取艺人总数
            Map<String, Long> total = starTvService.getStarTotal();
            resultMap.put("total",total.get("total"));

        }catch (IllegalArgumentException ie) {
            success = 1;
            message = ie.getMessage();
            resultMap.put("message", message);
        }catch (Exception e) {
            success = 1;
            message = "未知异常，查询艺人数据失败";
            logger.error(message, e);
            resultMap.put("message", message);
        }
            resultMap.put("error_code", success);

            return resultMap;
    }

    /**
     * 获取作品列表
     * @param paramModel
     * @return
     */
    @GetMapping("/queryTvInfo")
    public Map<String, Object> queryTvInfo(ParamModel paramModel) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int success = 0;
        String message = "";
        try {
            //校验账户是否存在
            String verifyStatus = isVerifyPass(paramModel);
            if(StringUtils.isNotEmpty(verifyStatus)){
                throw new IllegalArgumentException(verifyStatus);
            }
            //设置默认page=1，rows=100
            setModelParam(paramModel);
            //获取列表记录
            List<TvModel> tvList = starTvService.getTvInfo(paramModel);
            resultMap.put("data", tvList);
            //获取作品总数
            Map<String, Long> total = starTvService.getTvTotal();
            resultMap.put("total",total.get("total"));

        }catch (IllegalArgumentException ie) {
            success = 1;
            message = ie.getMessage();
            resultMap.put("message", message);
        }catch (Exception e) {
            success = 1;
            message = "未知异常，查询作品数据失败";
            logger.error(message, e);
            resultMap.put("message", message);

        }
            resultMap.put("error_code", success);

        return resultMap;
    }

    /**
     * 获取演员、导演、编剧对应的作品信息
     * type 1:演员，2：导演，3：编剧
     * @param paramModel
     * @return
     */
    @GetMapping("/queryActorWorksInfo")
    public Map<String, Object> queryActorWorksInfo(ParamModel paramModel) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int success = 0;
        String message = "";
        try {
            //校验账户是否存在
            String verifyStatus = isVerifyPass(paramModel);
            if(StringUtils.isNotEmpty(verifyStatus)){
                throw new IllegalArgumentException(verifyStatus);
            }
            //设置默认page=1，rows=100
            setModelParam(paramModel);
            if(StringUtils.isEmpty(paramModel.getType())){
                throw new IllegalArgumentException("请输入演员职业类型");
            }
            if(paramModel.getType().equals("1")){
                paramModel.setTableName("tab_tv_actors");
            }else if(paramModel.getType().equals("2")){
                paramModel.setTableName("tab_director_works");
            }else if(paramModel.getType().equals("3")){
                paramModel.setTableName("tab_screenwriter_works");
            }
            //获取列表记录
            List<StarWorksModel> actorWorksList = starTvService.getActorWorksInfo(paramModel);
            resultMap.put("data", actorWorksList);
            //获取总数
            Map<String, Long> total = starTvService.getWorksTotal(paramModel.getTableName());
            resultMap.put("total",total.get("total"));

        }catch (IllegalArgumentException ie) {
            success = 1;
            message = ie.getMessage();
            resultMap.put("message", message);
        }catch (Exception e) {
            success = 1;
            message = "未知异常，查询艺人对应作品失败";
            logger.error(message, e);
            resultMap.put("message", message);
        }
        resultMap.put("error_code", success);

        return resultMap;
    }


    /**
     * 获取艺人获奖信息
     * @param paramModel
     * @return
     */
    @GetMapping("/queryActorAwardInfo")
    public Map<String, Object> queryActorAwardInfo(ParamModel paramModel) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int success = 0;
        String message = "";
        try {
            //校验账户是否存在
            String verifyStatus = isVerifyPass(paramModel);
            if(StringUtils.isNotEmpty(verifyStatus)){
                throw new IllegalArgumentException(verifyStatus);
            }
            //设置默认page=1，rows=100
            setModelParam(paramModel);
            //获取列表记录
            List<StarAwardModel> actorAwardList = starTvService.getActorAwardInfo(paramModel);
            resultMap.put("data", actorAwardList);
            //获取总数
            Map<String, Long> total = starTvService.getTotal("basicinfo.tab_actor_award");
            resultMap.put("total",total.get("total"));

        }catch (IllegalArgumentException ie) {
            success = 1;
            message = ie.getMessage();
            resultMap.put("message", message);
        }catch (Exception e) {
            success = 1;
            message = "未知异常，查询艺人获奖数据失败";
            logger.error(message, e);
            resultMap.put("message", message);
        }
            resultMap.put("error_code", success);

        return resultMap;
    }


    /**
     * 获取作品获奖信息
     * @param paramModel
     * @return
     */
    @GetMapping("/queryWorksAwardInfo")
    public Map<String, Object> queryWorksAwardInfo(ParamModel paramModel) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int success = 0;
        String message = "";
        try {
            //校验账户是否存在
            String verifyStatus = isVerifyPass(paramModel);
            if(StringUtils.isNotEmpty(verifyStatus)){
                throw new IllegalArgumentException(verifyStatus);
            }
            //设置默认page=1，rows=100
            setModelParam(paramModel);
            //获取列表记录
            List<TvAwardModel> worksAwardList = starTvService.getWorksAwardInfo(paramModel);
            resultMap.put("data", worksAwardList);
            //获取总数
            Map<String, Long> total = starTvService.getTotal("basicinfo.tab_works_award");
            resultMap.put("total",total.get("total"));

        }catch (IllegalArgumentException ie) {
            success = 1;
            message = ie.getMessage();
            resultMap.put("message", message);
        }catch (Exception e) {
            success = 1;
            message = "未知异常，查询作品获奖数据失败";
            logger.error(message, e);
            resultMap.put("message", message);
        }
            resultMap.put("error_code", success);


        return resultMap;
    }

     /**
     * 获取艺人小土指数信息
     * @param paramModel
     * @return
     */
    @GetMapping("/queryStarStatLevelInfo")
    public Map<String, Object> queryStarStatLevelInfo(ParamModel paramModel) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int success = 0;
        String message = "";
        try {
            //校验账户是否存在
            String verifyStatus = isVerifyPass(paramModel);
            if(StringUtils.isNotEmpty(verifyStatus)){
                throw new IllegalArgumentException(verifyStatus);
            }
            if(StringUtils.isEmpty(paramModel.getStartDate())){
                throw new IllegalArgumentException("请输入开始时间");
            }
            if(StringUtils.isEmpty(paramModel.getEndDate())){
                throw new IllegalArgumentException("请输入结束时间");
            }
            //设置默认page=1，rows=100
            setModelParam(paramModel);
            //获取列表记录
            List<StarStatLevelModel> starStatLevelList = starTvService.getStarStatLevelInfo(paramModel);
            resultMap.put("data", starStatLevelList);
            //获取总数
            Map<String, Long> total = starTvService.getTotal("stat.tab_star_stat_level");
            resultMap.put("total",total.get("total"));

        }catch (IllegalArgumentException ie) {
            success = 1;
            message = ie.getMessage();
            resultMap.put("message", message);
        }catch (Exception e) {
            success = 1;
            message = "未知异常，查询艺人指数数据失败";
            logger.error(message, e);
            resultMap.put("message", message);
        }
            resultMap.put("error_code", success);

        return resultMap;
    }

    /**
     * 获取作品播放量、热度数据信息
     * @param paramModel
     * @return
     */
    @GetMapping("/queryTvPlayHeatInfo")
    public Map<String, Object> queryTvPlayHeatInfo(ParamModel paramModel) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int success = 0;
        String message = "";
        try {
            //校验账户是否存在
            String verifyStatus = isVerifyPass(paramModel);
            if(StringUtils.isNotEmpty(verifyStatus)){
                throw new IllegalArgumentException(verifyStatus);
            }
            //添加指定数据
            //千万注意，播放量接口需要单独添加
            /*paramModel.setTvId("1680389,1447328");
            paramModel.setStarId("95051,95418,100096,37159,100666,2000,92351,2084,93264,36854,97749,15425,149246," +
                    "1502952,116284,127516,107425,1509372,1495188,43663,119184,137253,1507713,54615," +
                    "1509373,11112,111816,94300,92690,11737,36442,61708,2425,1959,33686,4726,11117," +
                    "2642,1779, 1102,10810,37872,11845,21958,10614,43357,97945,39232,97381,92552,4960," +
                    "18456,92315,3203,144282,92730,36755,45042,1509047,1509048,107232,13720,18606,39765," +
                    "129563,22463,1507801,138358,98376,135131,1509385,1509386,162343,42622,4480, " +
                    "20219,1509387,119514,117647,1509046");*/
            //获取列表记录
            List<TvPlayCountModel> tvPlayHeatList = starTvService.getTvPlayHeatInfo(paramModel);
            resultMap.put("data", tvPlayHeatList);
            //获取总数
            Map<String, Long> total = starTvService.getTvPlayHeatTotal();
            resultMap.put("total",total.get("total"));


        }catch (IllegalArgumentException ie) {
            success = 1;
            message = ie.getMessage();
            resultMap.put("message", message);
        }catch (Exception e) {
            success = 1;
            message = "未知异常，查询作品播放量和热度数据失败";
            logger.error(message, e);
            resultMap.put("message", message);
        }
            resultMap.put("error_code", success);

        return resultMap;
    }


    /**
     * 校验账户是否存在
     * @param model
     * @return
     */
    public String isVerifyPass(ParamModel model){
        String message = "";
        try{
        String userName = model.getUserName();
        String passWord = model.getPassWord();
        int userNum = starTvService.getVerifyUser(userName,passWord);
        if(userNum==0){
            throw new IllegalArgumentException("请输入用户名和密码验证身份");
        }
        } catch (IllegalArgumentException ie) {
            message = ie.getMessage();
        }
            return  message;
    }


    public ParamModel setModelParam(ParamModel model){
        Integer page = model.getPage();
        Integer rows = model.getRows();
        if(page == null){
            model.setPage(1);
        }
        if(rows == null){
            model.setRows(100);
        }
        //千万注意，播放量接口需要单独添加
       /* model.setTvId("1680389,1447328");
        model.setStarId("95051,95418,100096,37159,100666,2000,92351,2084,93264,36854,97749,15425,149246," +
                        "1502952,116284,127516,107425,1509372,1495188,43663,119184,137253,1507713,54615," +
                        "1509373,11112,111816,94300,92690,11737,36442,61708,2425,1959,33686,4726,11117," +
                        "2642,1779, 1102,10810,37872,11845,21958,10614,43357,97945,39232,97381,92552,4960," +
                        "18456,92315,3203,144282,92730,36755,45042,1509047,1509048,107232,13720,18606,39765," +
                        "129563,22463,1507801,138358,98376,135131,1509385,1509386,162343,42622,4480, " +
                        "20219,1509387,119514,117647,1509046");*/
        return  model;
    }
}
